﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;

namespace RC1.admin
{
    public partial class student : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            errorLabel.Visible = false;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            addStudent();
        }

        private void addStudent()
        {
            String firstName = firstnameTextBox.Text;
            String lastName = lastnameTextBox.Text;
            String email = emailTextBox.Text;
            String passWord = passwordTextBox.Text;
            String stringPhoneNumber = phoneNumberTextBox.Text;
            
            int phoneNumber = -1;
            if (stringPhoneNumber.Length > 0)
            {
                phoneNumber = Convert.ToInt32(stringPhoneNumber);
            }
            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            var preUser = (from u in db.lpusers
                           where u.user_email == email
                           select u).SingleOrDefault();
            if (preUser == null)
            {

                lpuser student = new lpuser();
                student.user_email = email;
                student.user_password = passWord;
                student.user_level = 0;
                db.lpusers.AddObject(student);
                db.SaveChanges();

                var id = (from u in db.lpusers
                          where u.user_email == email
                          select u.user_id).SingleOrDefault();

                user_info student2 = new user_info();
                student2.user_id = id;
                student2.user_info_first_name = firstName;
                student2.user_info_last_name = lastName;
                if (phoneNumber != -1)
                {
                    student2.user_info_phone_number = phoneNumber;
                }
                db.user_info.AddObject(student2);
                db.SaveChanges();
                Server.Transfer("student.aspx");
            }
            else
            {
                printError("T-postur longu skrásettur");
            }
        }

        protected String getButtons(int user_id, String name)
        {
            String temp = "[";
            temp += "<a href='edit_user.aspx?id=" + user_id + "&p=s'><img src='../images/edit-icon.png' alt='Broyt' width='20' height='20'/></a> | ";
            temp += "<a href='delete_user.aspx?id=" + user_id + "' onclick='return confirm(\"Skal brúkarin " + name + " strikas?\")'>Strika</a>";
            temp += "]";
            return temp;
        }

        private void printError(String s)
        {
            errorLabel.Text = "<tr><td colspan='2' align='center'><span style='color:red;'>" + s + "</span></td></tr>";
            errorLabel.Visible = true;
        }
    }
}